package com.rangers.manage.email.strategy;

import org.springframework.util.Assert;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine;

import com.rangers.manage.email.domain.EmailDto;

public class ThymeleafStrategy implements EmailStrategy {

	private SpringTemplateEngine springTemplateEngine;

	private Context context;
	/**
	 * 相对路径名：/thymeleaf/emailTemplate.html
	 */
	private String templateName;

	public ThymeleafStrategy setSpringTemplateEngine(SpringTemplateEngine springTemplateEngine) {
		this.springTemplateEngine = springTemplateEngine;
		return this;
	}

	/**
	 * 创建策略
	 * 
	 * @param velocityContext
	 *            如：
	 *            <li>Context context = new Context();</li>
	 *            <li>context.setVariable("name", "fangjie");</li>
	 *            <li>context.setVariable("text", "使用Thymeleaf构建Email消息");</li>
	 * @param templateName
	 *            <li>模板相对路径名:/thymeleaf/emailTemplate.html</li>
	 */
	public ThymeleafStrategy(Context context, String templateName) {
		this.context = context;
		this.templateName = templateName;
	}

	@Override
	public String getMsgContent(EmailDto vo) {
		Assert.notNull(this.springTemplateEngine, "springTemplateEngine cannot be null:thymeleaf的模板引擎没有配置");
		String content = this.springTemplateEngine.process(this.templateName, context);
		return content;
	}
}
